﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComFun;

namespace ScheduleAutoAction
{   
    /// <summary>
    /// 自动任务计划选项
    /// </summary>
    [XmlRootAttribute("ScheduleShutdownOption", Namespace = "ScheduleAutoAction", IsNullable = false)]
    public class ScheduleShutdownOption : XmlSerializeBase
    {
        /// <summary>
        /// 是否开启自动清理任务计划功能
        /// </summary>
        private bool _enabled = false;
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// 任务上午执行时间
        /// </summary>
        private DateTime _executeTimeAM = new DateTime(2019, 8, 6, 12, 0, 0);
        public DateTime ExecuteTimeAtAM
        {
            get { return _executeTimeAM; }
            set { _executeTimeAM = value; }
        }

        /// <summary>
        ///任务下午执行时间
        /// </summary>
        private DateTime _executeTimePM = new DateTime(2019, 8, 6, 18, 0, 0);
        public DateTime ExecuteTimeAtPM
        {
            get { return _executeTimePM; }
            set { _executeTimePM = value; }
        }

        public ScheduleShutdownOption()
        {
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

       public override bool Load()
        {
            ScheduleShutdownOption obj = (ScheduleShutdownOption)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._enabled = obj._enabled;
                this._executeTimeAM = obj._executeTimeAM;
                this._nVer = obj._nVer;
                this._executeTimePM = obj._executeTimePM;         
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("自动清理任务计划选项: ");
            builder.Append("[是否启用]:" + (_enabled ? "是" : "否"));
            builder.Append("; [上午执行时间]:" + _executeTimeAM.ToString());
            builder.Append("; [下午执行时间]:" + _executeTimePM.ToString());    
            return builder.ToString();
        }
    }
}
